package com.tanhua.listener;

import com.tanhua.commons.template.ContentCensorTemplate;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.PublishApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RocketMQMessageListener(topic = "tanhua-publish", consumerGroup = "tanhua-publish-group")
public class PublishMessageListener implements RocketMQListener<String> {

    @Reference
    private PublishApi publishApi;

    @Autowired
    private ContentCensorTemplate contentCensorTemplate;

    @Override
    public void onMessage(String message) {
        log.info("==动态审核【开始】==");
        log.info("publishId：{}", message);

        //查找动态
        Publish publish = publishApi.findById(message);

        //内容审核
        if (publish.getState() == 0) {
            //审核文本内容
            boolean textOk = contentCensorTemplate.textCensor(publish.getTextContent());
            //审核图片
            boolean imageOk = contentCensorTemplate.imageBatchCensor(publish.getMedias());
            //设置审核结果
            publish.setState(textOk && imageOk ? 1 : 2);
            //更新到数据库
            publishApi.saveState(publish);
        }
        log.info("====动态审核【完成】====");
    }
}
